<?php 
header("Content-Type: text/html; charset=utf-8");
session_start();
if (!isset ($_SESSION["id"])) {
	session_destroy();
	echo "<script language='javascript'>top.location.href = 'Login.php';</script>";
	exit;
}

include_once 'public/DB_Oracle.php';
include_once 'public/page/page.class.php';
$DBsql = new DB_Oracle ();
//查询所以警员
$sql="select * from sys_admin";
$rows=$DBsql->fetch_all_array($sql);
//查询所有的派出所
$sql="select dd.department_index, dd.department_name
		  from dep_department dd
		 order by dd.department_index";
$dedp=$DBsql->fetch_all_array($sql);
//用户组
$sql=" select * from sys_permission sp where sp.permission_index!=1";
$groups=$DBsql->fetch_all_array($sql);
//区域
$sql="select * from dep_area";
$areas=$DBsql->fetch_all_array($sql);
//管理员列表
$pageSize = 20;
$total=count($rows)== 0 ? 1 : count($rows);
//第一个0显示数字  第二个1显示概况 第三个显示下拉 第四个显示数字数量
pageft($total, $pageSize, 1, 1, 1, 5);
$displaypg+=$firstcount;
$sql="select *
  	  from (select rownum r,
  	  		   sa.user_index,
               sa.user_id,
               sa.user_name,
               sa.user_tel,
               sp.permission_name,
               sa.user_sn,
               sp.web_ui,
               dd.department_name,
               da.area_name
          from sys_admin      sa,
               sys_permission sp,
               dep_department dd,
               dep_area       da
         where sa.permission_index = sp.permission_index
           and sa.department_index = dd.department_index
           and dd.area_index = da.area_index) c
	 where c.r > $firstcount
	   and c.r <= $displaypg";
$rows=$DBsql->fetch_all_array($sql);
?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/validform.css" rel="stylesheet" type="text/css" />
<link href="public/page/css/css.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
<script language="javascript" type="text/javascript" src="js/Validform.js"></script>
<script language="javascript" type="text/javascript" src="js/public.js"></script>
<script language='javascript'><!--
var flag=false;
	function sltItem(e){
		if(e.checked){
			document.getElementById("hidd").value+=",";
			document.getElementById("hidd").value+=e.value;
		}else{
			var str=document.getElementById("hidd").value;
			var checkValu=","+e.value;
			var valu=str.replace(checkValu,"");
			document.getElementById("hidd").value=valu;
		}
	}
	function setselectstate(){
		   if(flag){
			   var $aray = document.getElementsByName("selectID");
			   for(var i=0;i<$aray.length;i++){
			   	if($aray[i].disabled!=true){
						$aray[i].checked = false;
					}
				}
			   flag=false;
		   }else{
			   var $aray = document.getElementsByName("selectID");
			   for(var i=0;i<$aray.length;i++){
			   	if($aray[i].disabled!=true){
						$aray[i].checked = true;
					}
				}
				flag=true;
		   }
		   sltAll();
		}
	function sltAll(){
		var $aray = document.getElementsByName("selectID");
		var $str="";
		var sub=",";
		for(var i=0;i<$aray.length;i++){
			if($aray[i].checked){
				$str+=sub+$aray[i].value;
			}
		}
		if($str==","){
			$("#hidd").val("");
		}else{
			$("#hidd").val($str);
		}
	}
	function delChecked(){
		var id=$('#hidd').val();
		if(id==""){
			alert('请选择要删除的人员');
		}else{
			sn=id.substr(1,id.length);
			delRow(sn);
		}
	}
	//单位联动
	function changeWork(){
		var areaId=$('#area').val();
		$.ajax({
			type:'POST',
			url:'manageRole.php',
			data:'act=selectArea&sn='+areaId,
			dataType:'json',
			success:function(data){
				var str="";
				$.each(data,function(index,array){
					str+='<option value="'+array['DEPARTMENT_INDEX']+'">'+array['DEPARTMENT_NAME']+'</option>';
				});
				$('#work').html(str);
			}
		});
	}
	//修改警员信息
	function editRow(rowId){
		var valu=$('#'+'List'+rowId).children("td");	
		$('#userNumUp').val(valu[2].firstChild.data);
		$('#userNameUp').val(valu[3].firstChild.data);		
		$('#userPhoUp').val(valu[5].firstChild.data);
		$('#phoneNumUp').val(valu[4].firstChild.data);
		$('#hiddUp').val(valu[1].firstChild.data);
		
		
		var count=$('#userGroupUp option').length;
		for(var i=0;i<count;i++){
			if($('#userGroupUp').get(0).options[i].text==valu[6].firstChild.data){
				$('#userGroupUp').get(0).options[i].selected=true;
				break;
			}
		}
		count=$('#areaUp option').length;
		for(var i=0;i<count;i++){
			if($('#areaUp').get(0).options[i].text==valu[8].firstChild.data){
				$('#areaUp').get(0).options[i].selected=true;
				break;
			}
		}
		count=$('#workUp option').length;
		for(var i=0;i<count;i++){
			if($('#workUp').get(0).options[i].text==valu[7].firstChild.data){
				$('#workUp').get(0).options[i].selected=true;
				break;
			}
		}
		POPdiv('UPdiv');
	}
	//删除区号信息
	function delRow(rowId){
		if(!confirm('确定删除吗?')){
			return false;
		}
		$.ajax({
			type:'POST',
			url:'manageRole.php',
			data:'act=del&sn='+rowId,
			success:function(data){
				if(data=='y'){
					$.Showmsg('删除成功！');
					setTimeout(function(){
							$.Hidemsg();
							window.location.reload();
						},1000);
				}else{
					$.Showmsg('删除失败！');
				}
			}	
		});
	}
	
	$(function(){
		$("#adduser").Validform({
			 tiptype:2,
			   ajaxurl:"manageRole.php?act=add",
			   callback:function(data){
					if(data.status=="y"){
						$.Showmsg(data.info);
						setTimeout(function(){
							$.Hidemsg();
							window.location.reload();
						},1000);
					}else{
						$.Showmsg(data.info);
					}
			   }
		});
	$("#upuser").Validform({
		 tiptype:2,
		   ajaxurl:"manageRole.php?act=up",
		   callback:function(data){
				if(data.status=="y"){
					$('#userNum').val("");
					$('#userName').val("");		
					$('#userPho').val("");
					$('#phoneNum').val("");
					$.Showmsg(data.info);
					$.Showmsg(data.info);
					setTimeout(function(){
						$.Hidemsg();
						window.location.reload();
					},1000);
				}else{
					$.Showmsg(data.info);
				}
		   }
	});
});
--></script>
<!--管理列表开始-->
<div class="manageTab">
  <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" class="usrTab">
    <tr>
      <td width="8" height="25"><img src="images/user_01.jpg" height="25" width="8" /></td>
      <td style="background:#3980b2;" valign="middle" height="16" ><div class="use_righgDiv"><span>
        <input type="checkbox" id="checkall" name="" value="" onclick="setselectstate()" />
        全选</span><span><img src="images/001.gif" height="14" width="14" /><a href="javascript:void(null)" onclick="javascript:POPdiv('POPdiv')">新增</a></span><span><img src="images/114.gif" height="14" width="14" /><a onclick="delChecked()" href="javascript:void(null)">批量删除</a></span><span><img src="images/qx.gif" /> </span></div>
        <div class="use_leftDiv">
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td  width="22"><img src="images/311.gif" height="16" width="16" /></td>
              <td align="left"><span class="tdTit">用户管理列表</span></td>
            </tr>
          </table>
        </div></td>
      <td width="8" height="25"><img src="images/user_02.gif" height="25" width="8" /></td>
    </tr>
    <tr>
      <td style="background:#3980b2;" >&nbsp;</td>
      <td>
        <table width="100%" border="0" cellspacing="1" cellpadding="0" align="center" class="usrList" id="List">
          <tr>
            <td class="td90b2">选择</td>
            <td class="td90b2">编号</td>
            <td class="td90b2">用户名</td>
            <td class="td90b2">姓名</td>
            <td class="td90b2">人员号</td>
            <td class="td90b2">手机号</td>
            <td class="td90b2">用户组</td>
            <td class="td90b2">派出所</td>
            <td class="td90b2">区域</td>
            <td class="td90b2">编辑</td>
            <td class="td90b2">删除</td>
          </tr>
          <?php if(count($rows)!=0){
          		foreach ($rows as $ros){
          	?>
	          <tr id="List<?php echo $ros['USER_INDEX'];?>">
	            <td class="tdfff"><input type='checkbox' onclick='sltItem(this)' <?php if($ros['USER_INDEX']==1){echo 'disabled=true';}?> name="selectID" class="STYLE2" value="<?php echo $ros['USER_INDEX'];?>" /></td>
	            <td class="tdfff"><?php echo $ros['USER_INDEX'];?></td>
	            <td class="tdfff"><?php echo $ros['USER_ID'];?></td>
	            <td class="tdfff"><?php echo $ros['USER_NAME'];?></td>
	            <td class="tdfff"><?php echo $ros['USER_SN'];?></td>
	            <td class="tdfff"><?php echo $ros['USER_TEL'];?></td>
	            <td class="tdfff"><?php echo $ros['PERMISSION_NAME'];?></td>
	            <td class="tdfff"><?php echo $ros['DEPARTMENT_NAME'];?></td>
	            <td class="tdfff"><?php echo $ros['AREA_NAME'];?></td>
	            <td class="tdfff"><div align="center"><img src="images/037.gif" width="9" height="9" /><span class="STYLE1"> [</span><a href="javascript:void(null)" <?php if($ros['USER_INDEX']!=1){echo "onclick='editRow(\"".$ros['USER_INDEX']."\")'";}?>>编辑</a><span class="STYLE1">]</span></div></td>
	            <td class="tdfff"><div align="center"><img src="images/010.gif" width="9" height="9" /><span class="STYLE1"> [</span><a href="javascript:void(null)" <?php if($ros['USER_INDEX']!=1){echo "onclick='delRow(\"".$ros['USER_INDEX']."\")'";}?>>删除</a><span class="STYLE1">]</span></div></td>
	          </tr>
          <?php }}else{echo '暂无数据！';}?>
          <tr>
            <td colspan="11" class="tdfff">&nbsp;</td>
          </tr>
        </table>
      </td>
      <td style="background:#3980b2;">&nbsp;</td>
    </tr>
    <tr>
    	 <td height="24" width="8"><img src="images/user_03.gif" height="24" width="8" /></td>
    <td style="background:#3980b2;">
    <div class="green-black"><?php echo $pagenav;?></div>
	</td>
    <td height="24" width="8"><img src="images/user_04.gif" height="24" width="8" /></td>
    </tr>
  </table>
</div>
<!--管理列表结束-->
<input type="hidden" id='hidd' value="" />
<!--弹出层新建用户开始-->
      <div id="POPdiv" class="POPdiv">
        <div class="POPdiv_top"><a href="javascript:void(null)" onclick="ClosePOPdiv('POPdiv')"><img src="images/ico_close.jpg" alt="关闭" /></a>增加管理员</div>
        <div class="POPdiv_content">
			<form class="adduser" id="adduser">
			  <table border="0" cellpadding="5" cellspacing="0">
			    <tr>
			      <td width="80"><label for="userNum">用户名：</label></td>
			      <td><input type="text" ajaxurl="manageRole.php?act=valid" id="userNum" name='userNum' datatype="s" nullmsg="请输入用户名" errormsg='至少3~10个字符'/></td>
			      <td><span class="Validform_checktip">至少3~10个字符</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userName">真实姓名：</label></td>
			      <td><input type="text" id="userName" name='userName' datatype="s" nullmsg="请输入真实姓名" errormsg='至少3~10个字符'/></td>
			      <td><span class="Validform_checktip">至少3~10个字符</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userPwd">密码：</label></td>
			      <td><input type="text" name='userPwd' id="userPwd" datatype="*" nullmsg="请输入密码" errormsg='至少6~16个字符'/></td>
			      <td><span class="Validform_checktip">至少6~16个字符</span></td>
			    </tr>
			     <tr>
			      <td width="80"><label for="userPwd2">确认密码：</label></td>
			      <td><input type="text" name='userPwd2' id="userPwd2" recheck="userPwd" datatype="*" nullmsg="请再输入一次密码" errormsg='您两次输入的密码有一致'/></td>
			      <td><span class="Validform_checktip">至少6~16个字符</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="phoneNum">人员号：</label></td>
			      <td>
			      	<input type="text" value="" id='phoneNum' name="phoneNum" datatype="*" errormsg="请输入人员号"  nullmsg="人员号不能为空"/>
			      </td>
			      <td><span class="Validform_checktip">人员号不能为空</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userPho">手机号：</label></td>
			      <td>
			      	<input type="text" value="" id='userPho' name="userPho" datatype="m" errormsg="请输入您的手机号码"  nullmsg="手机号不能为空"/>
			      </td>
			      <td><span class="Validform_checktip">手机号不能为空</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userGroup">选择用户组：</label></td>
			      <td>
			      	<select id="userGroup" name='userGroup' datatype='select' errormsg='请选择用户组'>
			          <option value="">选择用户组</option>
			          <?php 
			          	foreach ($groups as $row){
			          		echo "<option value='".$row['PERMISSION_INDEX']."'>".$row['PERMISSION_NAME']."</option>";
			          	}
			          ?>
			        </select>
			      </td>
			      <td><span class="Validform_checktip"></span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="area">所在区域：</label></td>
			      <td><select id="area" name='area' onchange='changeWork()' datatype='select' errormsg='请选择区域'>
			          <option value="">选择区域</option>
			          <?php 
			          	foreach ($areas as $row){
			          		echo "<option value='".$row['AREA_INDEX']."'>".$row['AREA_NAME']."</option>";
			          	}
			          ?>
			        </select></td>
			      <td><span class="Validform_checktip"></span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="work">所在单位：</label></td>
			      <td><select id="work" name='work' datatype='select' errormsg='请选择派出所'>
			          <option value="">选择单位</option>
			          <?php 
			          	foreach ($dedp as $row){
			          		echo "<option value='".$row['DEPARTMENT_INDEX']."'>".$row['DEPARTMENT_NAME']."</option>";
			          	}
			          ?>
			        </select></td>
			      <td><span class="Validform_checktip"></span></td>
			    </tr>
			    <tr>
			      <td colspan="3"><input name="" id="add" type="submit" class="btnbg" value="添加用户" style="width:70px; margin-left:80px; color:#FFF" /></td>
			    </tr>
			  </table>
			</form>
        </div>
      </div>
<!--弹出层新建用户结束-->


<!--弹出层修改用户开始-->
      <div id="UPdiv" class="POPdiv">
        <div class="POPdiv_top"><a href="javascript:void(null)" onclick="ClosePOPdiv('UPdiv')"><img src="images/ico_close.jpg" alt="关闭" /></a>修改管理员信息</div>
        <div class="POPdiv_content">
          <form class="adduser" id="upuser">
			   <table border="0" cellpadding="5" cellspacing="0">
			    <tr>
			      <td width="80"><label for="userNumUp">用户名：</label></td>
			      <td><input type="text" id="userNumUp" name='userNumUp' datatype="s" nullmsg="请输入用户名" errormsg='至少3~10个字符'/></td>
			      <td><span class="Validform_checktip">至少3~10个字符</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userNameUp">真实姓名：</label></td>
			      <td><input type="text" id="userNameUp" name='userNameUp' datatype="s" nullmsg="请输入真实姓名" errormsg='至少3~10个字符'/></td>
			      <td><span class="Validform_checktip">至少3~10个字符</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userPwdUp">密码：</label></td>
			      <td><input type="text" name='userPwdUp' id="userPwdUp" datatype="*" nullmsg="请输入密码" errormsg='至少6~16个字符'/></td>
			      <td><span class="Validform_checktip">至少6~16个字符</span></td>
			    </tr>
			     <tr>
			      <td width="80"><label for="userPwdUp2">确认密码：</label></td>
			      <td><input type="text" name='userPwdUp2' id="userPwdUp2" recheck="userPwdUp" datatype="*" nullmsg="请再输入一次密码" errormsg='您两次输入的密码有一致'/></td>
			      <td><span class="Validform_checktip">至少6~16个字符</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="phoneNumUp">人员号：</label></td>
			      <td>
			      	<input type="text" value="" id='phoneNumUp' name="phoneNumUp" datatype="*" errormsg="请输入人员号"  nullmsg="人员号不能为空"/>
			      </td>
			      <td><span class="Validform_checktip">人员号不能为空</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userPhoUp">手机号：</label></td>
			      <td>
			      	<input type="text" value="" id='userPhoUp' name="userPhoUp" datatype="m" errormsg="请输入您的手机号码"  nullmsg="手机号不能为空"/>
			      </td>
			      <td><span class="Validform_checktip">手机号不能为空</span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="userGroupUp">选择用户组：</label></td>
			      <td>
			      	<select id='userGroupUp' name='userGroupUp' datatype='select' errormsg='请选择用户组'>
			          <option value="">选择用户组</option>
			          <?php 
			          	foreach ($groups as $row){
			          		echo "<option value='".$row['PERMISSION_INDEX']."'>".$row['PERMISSION_NAME']."</option>";
			          	}
			          ?>
			        </select>
			      </td>
			      <td><span class="Validform_checktip"></span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="areaUp">所在区域：</label></td>
			      <td><select id="areaUp" name='areaUp' onchange='changeWork()' datatype='select' errormsg='请选择区域'>
			          <option value="">选择区域</option>
			          <?php 
			          	foreach ($areas as $row){
			          		echo "<option value='".$row['AREA_INDEX']."'>".$row['AREA_NAME']."</option>";
			          	}
			          ?>
			        </select></td>
			      <td><span class="Validform_checktip"></span></td>
			    </tr>
			    <tr>
			      <td width="80"><label for="workUp">所在单位：</label></td>
			      <td><select id="workUp" name='workUp' datatype='select' errormsg='请选择派出所'>
			          <option value="">选择单位</option>
			          <?php 
			          	foreach ($dedp as $row){
			          		echo "<option value='".$row['DEPARTMENT_INDEX']."'>".$row['DEPARTMENT_NAME']."</option>";
			          	}
			          ?>
			        </select></td>
			      <td><span class="Validform_checktip"></span></td>
			    </tr>
			    <tr>
			      <td colspan="3"><input name="" id="add" type="submit" class="btnbg" value="修改用户" style="width:70px; margin-left:80px; color:#FFF" /></td>
			    </tr>
			  </table>
			  <input type="hidden" id="hiddUp" name="hiddUp"/> 
			</form>
        </div>
      </div>
<!--弹出层修改用户结束-->